
# 棋子的固定分数(固定子力)
chess_basic_value = {
	'King': 0,
	'Guard': 20,
	'Bishop': 20,
	'Knight': 45,
	'Cannon': 50,
	'Rock': 100,
	'Pawn': 10,
}

# 棋子的位置分数
position_value = {
	# 将(帅)
	'King' : [
		[0,	0,	0, 11, 15, 11,	0,	0,	0],	
		[0,	0,	0,	2,	2,	2,	0,	0,	0],	
		[0,	0,	0,	1,	1,	1,	0,	0,	0],	
		[0,	0,	0,	0,	0,	0,	0,	0,	0],	
		[0,	0,	0,	0,	0,	0,	0,	0,	0],	
		[0,	0,	0,	0,	0,	0,	0,	0,	0],	
		[0,	0,	0,	0,	0,	0,	0,	0,	0],	
		[0,	0,	0,	0,	0,	0,	0,	0,	0],	
		[0,	0,	0,	0,	0,	0,	0,	0,	0],	
		[0,	0,	0,	0,	0,	0,	0,	0,	0]
	],
	# 士(仕)
	'Guard' : [
		[0,	0,	0, 20,	0, 20,	0,	0,	0],
		[0,	0,	0,	0, 23,	0,	0,	0,	0],
		[0,	0,	0, 20,	0, 20,	0,	0,	0],	
		[0,	0,	0,	0,	0,	0,	0,	0,	0],	
		[0,	0,	0,	0,	0,	0,	0,	0,	0],	
		[0,	0,	0,	0,	0,	0,	0,	0,	0],	
		[0,	0,	0,	0,	0,	0,	0,	0,	0],
		[0,	0,	0,	0,	0,	0,	0,	0,	0],	
		[0,	0,	0,	0,	0,	0,	0,	0,	0],	
		[0,	0,	0,	0,	0,	0,	0,	0,	0]
	], 
	# 象(相)
	'Bishop':[
		[0,	0, 20,	0,	0,	0, 20,	0,	0],
		[0,	0,	0,	0,	0,	0,	0,	0,	0],
		[18, 0,	0,	0, 23,	0,	0,	0, 18],
		[0,	0,	0,	0,	0,	0,	0,	0,	0],
		[0,	0, 20,	0,	0,	0, 20,	0,	0],
		[0,	0,	0,	0,	0,	0,	0,	0,	0],
		[0,	0,	0,	0,	0,	0,	0,	0,	0],
		[0,	0,	0,	0,	0,	0,	0,	0,	0],
		[0,	0,	0,	0,	0,	0,	0,	0,	0],
		[0,	0,	0,	0,	0,	0,	0,	0,	0]
	],
	# 马
	'Knight':[
		[88, 85, 90, 88, 90, 88, 90, 85, 88],
		[85, 90, 92, 93, 78, 93, 92, 90, 85],
		[93, 92, 94, 95, 92, 95, 94, 92, 93],
		[92, 94, 98, 95, 98, 95, 98, 94, 92],
		[90, 98,101,102,103,102,101, 98, 90],
		[90,100, 99,103,104,103, 99,100, 90],
		[93,108,100,107,100,107,100,108, 93],
		[92, 98, 99,103, 99,103, 99, 98, 92],
		[90, 96,103, 97, 94, 97,103, 96, 90],
		[90, 90, 90, 96, 90, 96, 90, 90, 90]
	],
	# 车
	'Rock':[
		[194,206,204,212,200,212,204,206,194],
		[200,208,206,212,200,212,206,208,200],
		[198,208,204,212,212,212,204,208,198],
		[204,209,204,212,214,212,204,209,204],
		[208,212,212,214,215,214,212,212,208],
		[208,211,211,214,215,214,211,211,208],
		[206,213,213,216,216,216,213,213,206],
		[206,208,207,214,216,214,207,208,206],
		[206,212,209,216,233,216,209,212,206],
		[206,208,207,213,214,213,207,208,206]
	],
	# 炮
	'Cannon':[
		[ 96, 96, 97, 99, 99, 99, 97, 96, 96],
		[ 96, 97, 98, 98, 98, 98, 98, 97, 96],
		[ 97, 96,100, 99,101, 99,100, 96, 97],
		[ 96, 96, 96, 96, 96, 96, 96, 96, 96],
		[ 95, 96, 99, 96,100, 96, 99, 96, 95],
		[ 96, 96, 96, 96,100, 96, 96, 96, 96],
		[ 96, 99, 99, 98,100, 98, 99, 99, 96],
		[ 97, 97, 96, 91, 92, 91, 96, 97, 97],
		[ 98, 98, 96, 92, 89, 92, 96, 98, 98],
		[100,100, 96, 91, 90, 91, 96,100,100],
	],
	# 卒(兵)
	'Pawn':[
		[ 0,	0,	0,	0,	0,	0,	0,	0,	0],
		[ 0,	0,	0,	0,	0,	0,	0,	0,	0],
		[ 0,	0,	0,	0,	0,	0,	0,	0,	0],
		[ 7,	0,	7,	0, 15,	0,	7,	0,	7],
		[ 7,	0, 13,	0, 16,	0, 13,	0,	7],
		[14, 18, 20, 27, 29, 27, 20, 18, 14],
		[19, 23, 27, 29, 30, 29, 27, 23, 19],
		[19, 24, 32, 37, 37, 37, 32, 24, 19],
		[19, 24, 34, 42, 44, 42, 34, 24, 19],
		[ 9,	9,	9, 11, 13, 11,	9,	9,	9],
	],
}

win_score=10

def has_king(board, player):
	for _, chess in board.board_map.items():
		if(chess.player==player) and (chess.type=='King'):
			return True
	return False

def red_score(board):
	# 先判断当前下棋方是否被将死
	# 注意，当前下棋方被将死说明对面下的好，这时候要根据对面是什么方来给分数
	# 如果对面是红方，则返回正无穷或一个较大的数字，反之返回负无穷或一个较小的数字
	if not has_king(board, 'Red'):
		return -win_score
	if not has_king(board, 'Black'):
		return win_score

	# 评分方式，红方棋子减去黑方棋子的分数
	score = 0
	# 统计一下此时各方拥有的棋子数
	# redChess = {'King': 0,'Guard': 0,'Bishop': 0,'Knight': 0,'Cannon': 0,'Rock': 0,'Pawn': 0}
	# blackChess = {'King': 0,'Guard': 0,'Bishop': 0,'Knight': 0,'Cannon': 0,'Rock': 0,'Pawn': 0}
	
	for _, chess in board.board_map.items():
		x, y = chess.x, chess.y
		if chess.player=='Black':
			 x, y = 8-x, 9-y
		s = position_value[chess.type][y][x] + chess_basic_value[chess.type]
		if chess.player=='Black':
			 s = -s
		score = score + s
	return score / 1000

# def score(board, player):
# 	rs = red_score(board)
# 	return rs if player=='Red' else -rs
	